package com.qth.valet.mapper.provider;

import com.qth.valet.common.utils.StatusConstants;
import org.apache.ibatis.jdbc.SQL;

public class UserCouponProvider {

    public String selectCouponByStatus(Integer id, Integer status) {
        return new SQL() {{
            SELECT("c.id couponId,\n" +
                    " c.name,\n" +
                    " c.is_threshold,\n" +
                    " c.at_least,\n" +
                    " c.amount,\n" +
                    " c.start_date,\n" +
                    " c.expire_date,\n" +
                    " cu.id,\n" +
                    " cu.user_id,\n" +
                    " cu.order_id,\n" +
                    " cu.is_used,\n" +
                    " cu.receive_time,\n" +
                    " cu.use_time");
            FROM("coupon_user cu");
            LEFT_OUTER_JOIN("coupon c ON cu.coupon_id = c.id");
            WHERE("cu.user_id = " + id);
            // 动态添加条件
            if (status != null && status == StatusConstants.COUPON_NO_USE) {
                WHERE("cu.is_used = 0");
                WHERE("DATEDIFF(c.expire_date, CURDATE()) >= 0");
            }else if(status != null && status == StatusConstants.COUPON_USED){
                WHERE("cu.is_used = 1");
            }else if (status != null && status == StatusConstants.COUPON_EXPIRED) {
                WHERE("cu.is_used = 0");
                WHERE("DATEDIFF(c.expire_date, CURDATE()) < 0");
            }
        }}.toString();
    }

}
